package com.haoting.blogservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haoting.blogservice.entity.BlogContent;
import com.haoting.blogservice.service.BlogContentService;
import com.haoting.commonutils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tingting
 * @since 2020-12-23
 */
@RestController
@RequestMapping("/blogservice/content")
//@CrossOrigin
public class BlogContentController {

    @Autowired
    BlogContentService blogContentService;

    @GetMapping("findAll")
    public CommonResult findAll(){
        List<BlogContent> list =  blogContentService.list(null);
        return CommonResult.ok().data("list",list);
    }

    @GetMapping("findContentbyBlogId/{blogId}")
    public CommonResult findContentbyBlogId(@PathVariable String blogId){
        QueryWrapper<BlogContent> wrapper = new QueryWrapper<>();
        wrapper.eq("blog_id",blogId);
        List<BlogContent> blogContent = blogContentService.list(wrapper);
        return CommonResult.ok().data("blogcontent",blogContent);
    }

    @PostMapping("addContent")
    public CommonResult addContent(@RequestBody BlogContent blogContent){
        boolean save = blogContentService.save(blogContent);
        if(save){
            return CommonResult.ok();
        }else {
            return CommonResult.error();
        }
    }
    @PostMapping("updateContent")
    public CommonResult updateContent(@RequestBody BlogContent blogContent){
        boolean save = blogContentService.updateById(blogContent);
        if(save){
            return CommonResult.ok();
        }else {
            return CommonResult.error();
        }
    }
    @DeleteMapping("{blogId}")
    public CommonResult deleteContent(@PathVariable String blogId){
        boolean delete = blogContentService.removeById(blogId);
        if(delete){
            return CommonResult.ok();
        }else {
            return CommonResult.error();
        }
    }



}

